package com.situ.boot.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.boot.entity.AdminUserEntity;
import com.situ.boot.entity.Result;
import com.situ.boot.entity.User;
import com.situ.boot.entity.UserEntity;
import com.situ.boot.mapper.UserMapper;
import com.situ.boot.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import com.situ.boot.mapper.UserMapper;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements IUserService {
	@Autowired
	private UserMapper userMapper;


	@Override
	public Result selectByPage(Integer page, Integer limit, UserEntity user) {
		//1、使用PageHelper插件设置分页
		PageHelper.startPage(page, limit);
		//2、执行查询
		List<UserEntity> list = userMapper.selectByPage(user);
		System.out.println(list);
		//3、使用PageInfo对结果进行包装
		PageInfo pageInfo = new PageInfo(list);
		Integer count = (int) pageInfo.getTotal();//得到总数量
		System.out.println("pageInfo" + pageInfo);
		return Result.success("查询成功", pageInfo.getList(), count);
	}

	@Override
	public List<UserEntity> selectAll() {
		return userMapper.selectAll();
	}

	@Override
	public UserEntity selectById(Integer userId) {
		UserEntity user = userMapper.selectByPrimaryKey(userId);

		return user;
	}

	@Override
	public boolean updateUser(UserEntity user) {
		int count = userMapper.updateByPrimaryKey(user);
		System.out.println(user);
		System.out.println("count = " +count);
		return count == 1 ? true : false;
	}

	@Override
	public boolean deleteById(Integer uid) {
		int count = userMapper.deleteByPrimaryKey(uid);
		System.out.println("count = " + count);
		return count == 1 ? true :false;
	}

	@Override
	public List<User> selectBySup(Integer uid) {
		List<User> list = userMapper.selectBySup(uid);
		return list;
	}

	@Override
	public boolean insertUser(UserEntity user) {
		int count = userMapper.insert(user);
		return count == 1 ? true : false;


	}

	@Override
	public User selectByTeamUserId(Integer uid) {
		User user = userMapper.selectByTeamUserId(uid);

		return user;
	}

	@Override
	public AdminUserEntity login(String username, String password) {
		return userMapper.login(username, password);
	}


}
